package com.fjnldx.mes.mapper.productionFigures;

import com.fjnldx.mes.domain.domainReq.DateSectionPageQueryReq;
import com.fjnldx.mes.domain.domainReq.DateSectionQueryReq;
import com.fjnldx.mes.domain.domainReq.HistoricalEmployment;
import com.fjnldx.mes.domain.domainRes.XsHistory;
import com.fjnldx.mes.domain.pojo.MesNewscjh;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface ProductionFiguresMapper {

    @Select("select count(*) cs from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='mes_database' and TABLE_NAME=#{tableName}")
    Integer isHaveDataByXs(@Param("tableName") String tableName);

    XsHistory getHistoryTrackData(DateSectionQueryReq dateSectionReq);

    List<Map<String, String>> getHistoryTrackDataByPage(DateSectionPageQueryReq dateSectionQueryReq);

    /**
     * 查询历史用工记录
     */
    @Select("<script>" +
            "SELECT a.* FROM " +
            "(SELECT org,cj,gongxu,DATE_FORMAT(time,'%Y-%m-%d') time,bc,count(DISTINCT dcg) sjyg,count(DISTINCT dcg)*10000/90816 wdyg FROM mes_newscjh GROUP BY org,cj,bc,gongxu,DATE_FORMAT(time,'%Y-%m-%d') ) a " +
            "WHERE 1=1 " +
            "<if test='org!=null'> AND a.org=#{org} </if>" +
            "<if test='cj!=null'> AND a.cj=#{cj} </if>" +
            "<if test='gongxu!=null'> AND a.gongxu=#{gongxu} </if>" +
            "<if test='bc!=null'> AND a.bc=#{bc} </if>" +
            "<if test='time!=null'> AND DATEDIFF(#{time},date(a.time))=0 </if>" +
            " limit ${page*size},#{size} " +
            "</script>")
    List<HistoricalEmployment> queryHistoricalEmployment(HistoricalEmployment historicalEmployment);

    /**
     * 获取历史用工记录total
     */
    @Select("<script>" +
            "SELECT count(*) FROM " +
            "(SELECT a.* FROM " +
            "(SELECT org,cj,gongxu,DATE_FORMAT(time,'%Y-%m-%d') time,bc,count(DISTINCT dcg) sjyg,count(DISTINCT dcg)*10000/90816 wdyg FROM mes_newscjh GROUP BY org,cj,bc,gongxu,DATE_FORMAT(time,'%Y-%m-%d') ) a " +
            "WHERE 1=1 " +
            "<if test='org!=null'> AND a.org=#{org} </if>" +
            "<if test='cj!=null'> AND a.cj=#{cj} </if>" +
            "<if test='gongxu!=null'> AND a.gongxu=#{gongxu} </if>" +
            "<if test='bc!=null'> AND a.bc=#{bc} </if>" +
            "<if test='time!=null'> AND DATEDIFF(#{time},date(a.time))=0 </if>" +
            ") b" +
            "</script>")
    int queryTotalHistoricalEmployment(HistoricalEmployment historicalEmployment);

    /**
     * 历史数据查询
     */
    @Select("<script>" +
            "SELECT * FROM mes_newscjh " +
            "WHERE 1=1 " +
            "<if test='org!=null'> AND org=#{org} </if>" +
            "<if test='cj!=null'> AND cj=#{cj} </if>" +
            "<if test='gongxu!=null'> AND gongxu=#{gongxu} </if>" +
            "<if test='devSn!=null'> AND devSn=#{devSn} </if>" +
            "<if test='bc!=null'> AND bc=#{bc} </if>" +
            "<if test='pz!=null'> AND pz=#{pz} </if>" +
            "<if test='time!=null'> AND DATEDIFF(#{time},date(time))=0 </if>" +
            "<if test='size!=-1'> limit ${page*size},#{size} </if>" +
            "</script>")
    List<MesNewscjh> queryHistoricalWorkersData(MesNewscjh mesNewscjh);

    /**
     * 历史数据查询total
     */
    @Select("<script>" +
            "SELECT count(*) FROM " +
            "(SELECT * FROM mes_newscjh " +
            "WHERE 1=1 " +
            "<if test='org!=null'> AND org=#{org} </if>" +
            "<if test='cj!=null'> AND cj=#{cj} </if>" +
            "<if test='gongxu!=null'> AND gongxu=#{gongxu} </if>" +
            "<if test='devSn!=null'> AND devSn=#{devSn} </if>" +
            "<if test='bc!=null'> AND bc=#{bc} </if>" +
            "<if test='pz!=null'> AND pz=#{pz} </if>" +
            "<if test='time!=null'> AND DATEDIFF(#{time},date(time))=0 </if>" +
            ") b" +
            "</script>")
    int queryTotalHistoricalWorkersData(MesNewscjh mesNewscjh);
}
